{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# JupyterHub's API\n",
    "\n",
    "JupyterHub has a [REST API](http://petstore.swagger.io/?url=https://raw.githubusercontent.com/jupyterhub/jupyterhub/master/docs/rest-api.yml#!/default/get_users):"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import requests\n",
    "hub_api = 'http://127.0.0.1:8081/hub/api/'\n",
    "\n",
    "token = \"result of token=$(jupyterhub token yourname)\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "from requests import HTTPError\n",
    "\n",
    "def api_request(path, method='get', data=None):\n",
    "    if data:\n",
    "        data = json.dumps(data)\n",
    "    \n",
    "    r = requests.request(method, hub_api + path,\n",
    "        headers={'Authorization': 'token %s' % token},\n",
    "        data=data,\n",
    "    )\n",
    "    try:\n",
    "        r.raise_for_status()\n",
    "    except Exception as e:\n",
    "        try:\n",
    "            info = r.json()\n",
    "        except Exception:\n",
    "            raise e\n",
    "        if 'message' in info:\n",
    "            # raise nice json error if there was one\n",
    "            raise HTTPError(\"%s: %s\" % (r.status_code, info['message'])) from None\n",
    "        else:\n",
    "            # raise original\n",
    "            raise e\n",
    "    if r.text:\n",
    "        return r.json()\n",
    "    else:\n",
    "        return None"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can list users and their status:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'admin': True,\n",
       "  'last_activity': '2016-05-06T11:53:51.627000',\n",
       "  'name': 'minrk',\n",
       "  'pending': None,\n",
       "  'server': '/user/minrk'},\n",
       " {'admin': False,\n",
       "  'last_activity': '2016-05-06T11:57:13.329254',\n",
       "  'name': 'takluyver',\n",
       "  'pending': None,\n",
       "  'server': None}]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "api_request('users')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also start user servers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'admin': True,\n",
       "  'last_activity': '2016-05-06T11:53:51.627000',\n",
       "  'name': 'minrk',\n",
       "  'pending': None,\n",
       "  'server': '/user/minrk'},\n",
       " {'admin': False,\n",
       "  'last_activity': '2016-05-06T11:57:29.285044',\n",
       "  'name': 'takluyver',\n",
       "  'pending': None,\n",
       "  'server': '/user/takluyver'}]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "api_request('users/takluyver/server', method='post')\n",
    "api_request('users')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And stop them:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[{'admin': True,\n",
       "  'last_activity': '2016-05-06T11:53:51.627000',\n",
       "  'name': 'minrk',\n",
       "  'pending': None,\n",
       "  'server': '/user/minrk'},\n",
       " {'admin': False,\n",
       "  'last_activity': '2016-05-06T11:57:30.145986',\n",
       "  'name': 'takluyver',\n",
       "  'pending': None,\n",
       "  'server': None}]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "api_request('users/takluyver/server', method='delete')\n",
    "api_request('users')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We can also see the proxy routing table:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'/': {'last_activity': '2016-05-06T11:56:29.543Z',\n",
       "  'target': 'http://127.0.0.1:8081'},\n",
       " '/user/minrk': {'last_activity': '2016-05-06T11:57:29.159Z',\n",
       "  'target': 'http://127.0.0.1:45683',\n",
       "  'user': 'minrk'}}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "api_request('proxy')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
